#!/bin/sh
### BEGIN INIT INFO
# Provides:          fpga
# Required-Start:    $local_fs $network $rpcbind
# Required-Stop:
# Default-Start:     S
# Default-Stop:
### END INIT INFO

major_fpga=224

fpga_load() {
    #Define all nodes:
    nodes=("Load")

    #载入驱动模块
    rmmod fpga
    insmod /home/root/fpga.ko major="${major_fpga}"

    i=0
    for node in ${nodes[@]}; do
    nodePath=/dev/fpga$node
    #删除已存在的节点
    rm -f $nodePath
    #创建节点
    mknod $nodePath c $major_fpga $i
    echo mknod $nodePath c $major_fpga $i
    chmod 666 $nodePath
    ((i+=1))
    done
}

fpga_program() {
    if [ -z $1 ]; then
        echo "program NAND.rbf to FPGA"
        cat /dev/mtdblock9 > /dev/fpgaLoad
    else
        echo "program $1 to FPGA..."
        cat $1 > /dev/fpgaLoad
    fi
}

save_rbf() {
    if [ -z $1 ]; then
        echo "Save fpga.rbf to NAND.rbf"
        cat fpga.rbf > /dev/mtdblock9
    else
        echo "Save $1 to NAND.rbf"
        cat $1 > /dev/mtdblock9
    fi
}

case "$1" in
  start)
    fpga_load
    ;;
  program)
    fpga_program $2
    ;;
  save)
    save_rbf $2
    ;;
  programandsave)
    fpga_program $2
    save_rbf $2
    ;;
  *)
    echo "Usage: fpga start: for load FPGA.ko Module and Program FPGA."
    echo "       fpga save *.rbf: save *.rbf file to nand partition."
    echo "       fpga program: load NAND.rbf partition to FPGA."
    echo "       fpga programandsave: program and save."
    exit 1
esac

exit 0

